package cc.fs.zhihuapp.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cc.fs.zhihuapp.R;
import cc.fs.zhihuapp.model.VideoBean;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;

/**
 * Created by fostion on 2015/11/3.
 */
public class SmbDataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private ArrayList<SmbFile> list;
    private OnItemClickListener onItemClickListener = null;

    public SmbDataAdapter(ArrayList<SmbFile> _list){
        list = _list;
    }

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_theme, parent, false);
        return new ItemViewHolder(v);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof  ItemViewHolder){
            ItemViewHolder vh = (ItemViewHolder)holder;
            vh.setData(list.get(position));
        }
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.title)
        TextView title;
        @InjectView(R.id.image)
        ImageView image;

        public ItemViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);

        }

        public void setData(final SmbFile smbFile) {
            try{
                if(smbFile.isDirectory()){
                    image.setImageResource(R.drawable.icon_dir);
                } else {
                    image.setImageResource(R.drawable.icon_file);
                }
                title.setText(smbFile.getName());
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onItemClickListener != null) {
                            onItemClickListener.onItemClick(smbFile);
                        }
                    }
                });
            }catch (Exception e){
                Log.e("---", "适配器出现错误");
            }

        }
    }

    public interface OnItemClickListener{
        void onItemClick(SmbFile smbFile);
    }

}
